﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.Script.Serialization;

namespace Utilities.Runtime.Serialization
{
    public class JsonSerializer : ISerializer
    {
        private JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();

        public string Serialize(object data)
        {
            return jsonSerializer.Serialize(data);
        }

        public T Deserialize<T>(string data)
        {
            return jsonSerializer.Deserialize<T>(data);
        }
        public Dictionary<string, object> Deserialize(string data)
        {
            return jsonSerializer.DeserializeObject(data) as Dictionary<string, object>;
        }

        public T DeserializeFile<T>(string path)
        {
            StreamReader reader = new StreamReader(path, Encoding.UTF8);
            string data = reader.ReadToEnd();
            return Deserialize<T>(data);
        }
        public Dictionary<string, object> DeserializeFile(string path)
        {
            StreamReader reader = new StreamReader(path, Encoding.UTF8);
            string data = reader.ReadToEnd();
            return Deserialize(data);
        }
    }
}
